var smileyName = ['005', '002', '004', '015'];
var smileyCode = ['', '', '', ''];
var smileyRegex = ['', '', '', ''];
var numSmileys = 4;
var imgStart = "<img src='http://i1273.photobucket.com/albums/y401/hamzaumar/NewSmileys/";
var imgStartNoPath = "<img src='";
var imgEnd = ".gif' />";

// Global variables:
var gMoreSmileysOpen = false;


function addToTextarea(n) {
    // textarea is named comment except for Forum, then its description

    // Get the img tag code, based on n:
    var imgtag = "";
    if (n < 4) {
        imgtag = imgStart + smileyName[n] + imgEnd;
    }
    else {
        if (window.moreSmileys !== undefined) imgtag = imgStartNoPath + moreSmileys.path + moreSmileys.smiley[n - 4].name + moreSmileys.ext + "' />";
    }

    tinymce.activeEditor.execCommand('mceInsertContent',false,imgtag);
}

function buildSmileyBar() {
    var sStart = " <a style='cursor:pointer' onclick='addToTextarea(";
    var sMid = ")'>";
    var sOut = "";

    var ix;
    for (ix = 0; ix < numSmileys; ix++) {
        sOut += sStart + ix + sMid + imgStart + smileyName[ix] + imgEnd + "</a>";
    }

    return sOut;
}

function getMoreSmileys() {
    var sStart = " <a style='cursor:pointer' onclick='addToTextarea(";
    var sMid = ")'>";
    var sOut = "";

    if (moreSmileys.ext == undefined) moreSmileys.ext = ".gif";  // Default to GIF

    if (window.moreSmileys !== undefined) {
        var ix;
        for (ix = 0; ix < moreSmileys.count; ix++) {
            sOut += sStart + (ix + 4) + sMid + imgStartNoPath + moreSmileys.path + moreSmileys.smiley[ix].name + moreSmileys.ext + "' /></a>";
        }
    }

    return sOut;
}

function showMoreSmileys() {
    if (gMoreSmileysOpen == false) {
        x$('.smileyBar').after('<div class="moreSmileys">MORE Smileys | <a style="cursor:pointer" onclick="closeMoreSmileys()">Close</a><br />' + getMoreSmileys() + '<br /><div style="font-size:12px">SMILEY Powered By <a href="http://vuhelp.pk/profile/Hami">Hami (VUHELP.pk)</a></div></div>');
        gMoreSmileysOpen = true;
    }
}

function closeMoreSmileys() {
    x$('div.moreSmileys').remove();
    gMoreSmileysOpen = false;
}

function encodeRE(s) {
    return s.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
}

// +++++++++++++++++++++++++++++++++++ READY FUNCTION ++++++++++++++++++++++++++++++++++++++++++++++++++
x$(document).ready(function() {

    var smileyBar = buildSmileyBar();

    // Inject the Smiley Bar, before the buttons that start with Add Comment or Add Reply:
    // x$("input[value^='Add Comment'], input[value^='Add Reply']").before("<span id='smileyBar' style='float:left'> Click to Insert: " + smileyBar + " | <a style='cursor:pointer' onclick='showMore()'>more</a> </span>");

    // Inject the Smiley Bar UNDER each textarea:
    x$("div.texteditor").after("<div class='smileyBar'> Click to Insert: " + smileyBar + " | <a style='cursor:pointer' onclick='showMoreSmileys()'>more</a> </div>");

    if (moreSmileys.ext == undefined) moreSmileys.ext = ".gif";  // Default to GIF
    //showSmileys();

});

